﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Model.Group;
using YouHP.Utility;
using YouHP.Logic;

namespace YouHP.Pages.User
{
    public class HomePage:UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }
        protected UserInfo _User;
        protected PatientRecordInfo PrData;
        protected List<VisitRecordInfo> LattestVisitRecordList;
        protected List<LogTopicInfo> HottestLogTopics;

        protected string fullIntegrityIcon = "√";
        protected string overallPercentage = "";
        protected string basicPercentage = "";
        protected string diseasePercentage = "";
        protected string symtomPercentage = "";
        protected string treatmentPercentage = "";
        protected string indicatorPercentage = "";

        protected string barLength = "";
        protected float overallCount = 0;
        protected float basicCount = 0;
        protected float diseaseCount = 0;
        protected float symtomCount = 0;
        protected float treatmentCount = 0;
        protected float indicatorCount = 0;


        private void HandleCommon()
        {
            this.SetTitle(UserData.UserName+"的主页");
            this.AddLocation("我的主页", "/Pages/Alpha/User/HomePage.aspx");
            this.AddLinkCss("/support/alpha/css/HomePage.css");
            _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            PrData = PatientBLL.Instance.GetPatientRecordInfo(_User.Id);

            LattestVisitRecordList = UserBLL.Instance.GetLattestVisitRecordList(_User.Id, 6);
            
        }
        private void HandlePost()
        {
           
        }
        public List<UserActionInfo> MyFriendActionList = new List<UserActionInfo>();

        public List<SymptomHistoryInfo> MyCurrentSymptomList;
        public List<IndicatorHistoryInfo> MyCurrentIndicatorList;
        public List<TreatmentHistoryInfo> MyCurrentTreatmentList;
        public List<TreatmentHistoryInfo> MyCurrentAllTreatementList;
        public struct SymptomStat { public int SymptomId; public string SymptomName; public int PatientCount;}
        public struct TreatmentStat { public int TreatmentId; public string TreatmentName; public int PatientCount;}
        public SymptomStat[] SymptomStatArray = new SymptomStat[100];
        public TreatmentStat[] TreatmentStatArray = new TreatmentStat[100];
        public DateTime dtLatestSymptomUpdateTime = new DateTime(1000, 1, 1); // 当前症状的最近更新时间
        public DateTime dtLatestIndicatorUpdateTime = new DateTime(1000, 1, 1); //  当前指标的最近更新时间
        public DateTime dtLatestTreatmentUpdateTime = new DateTime(1000, 1, 1); // 当前治疗的最近更新时间
        
        protected GroupInfo forum;
        //论坛最热N条贴
        protected List<TopicInfo> HottestTopicsList = new List<TopicInfo>();
        //论坛最新N条贴
        protected List<TopicInfo> LattestTopicsList = new List<TopicInfo>();
        //论坛新闻前N条
        protected List<GroupNewsInfo> NewsList = new List<GroupNewsInfo>();

        // 心情系统数据
        public List<EmotionTypeInfo> allEmotionTypes;
        public EmotionInfo emotionData;
        public string EmotionImgURL
        {
            get
            {
                if (emotionData == null || emotionData.EmotionTypeData == null || emotionData.EmotionTypeData.Img == "")
                {
                    // return a default one.
                    EmotionLogic emotionLogic = new EmotionLogic();
                    List<EmotionTypeInfo> allEmotionTypes = emotionLogic.GetAllEmotionTypes();
                    if (allEmotionTypes.Count == 0)
                        return "";
                    else
                        return allEmotionTypes[0].Img;
                }
                else
                    return emotionData.EmotionTypeData.Img;
            }
        }
        public string EmotionDescriptionStr
        {
            get
            {
                if (emotionData == null || emotionData.StatusDescription == "")
                    return "没有什么好说的...";
                else
                    return emotionData.StatusDescription;
            }
        }

        private void HandleGet()
        {
            MyCurrentSymptomList = PatientSymptomBLL.Instance.GetCurrentSymptomList(PrData.Id);
            MyCurrentIndicatorList = PatientIndicatorBLL.Instance.GetCurrentIndicatorList(PrData.Id);
            MyCurrentTreatmentList = PatientTreatmentBLL.Instance.GetBindingTreatmentList(PrData.Id);
            MyCurrentAllTreatementList = PatientTreatmentBLL.Instance.GetAllCurTreatments(PrData.Id);

            if (MyCurrentSymptomList != null && MyCurrentSymptomList.Count > 0)
            {
                foreach (SymptomHistoryInfo info in MyCurrentSymptomList)
                {
                    if (dtLatestSymptomUpdateTime.CompareTo(info.UpdateDate) < 0)
                    {
                        dtLatestSymptomUpdateTime = info.UpdateDate;
                    }
                }
            }

            if (MyCurrentIndicatorList != null && MyCurrentIndicatorList.Count > 0)
            {
                foreach (IndicatorHistoryInfo info in MyCurrentIndicatorList)
                {
                    if (dtLatestIndicatorUpdateTime.CompareTo(info.UpdateDate) < 0)
                    {
                        dtLatestIndicatorUpdateTime = info.UpdateDate;
                    }
                }
            }

            if (MyCurrentTreatmentList != null && MyCurrentTreatmentList.Count > 0)
            {
                foreach (TreatmentHistoryInfo info in MyCurrentTreatmentList)
                {
                    if (dtLatestTreatmentUpdateTime.CompareTo(info.UpdateDate) < 0)
                    {
                        dtLatestTreatmentUpdateTime = info.UpdateDate;
                    }
                }
            }

            int diseaseId = 0;
            if (UserData.Id > 0)
            {
                PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                if (prData.DiseaseId > 0)
                {
                    UserData.DiseaseId = prData.DiseaseId;
                    diseaseId = prData.DiseaseId;
                    if (prData.StartDate.CompareTo(Convert.ToDateTime("1800-01-01")) > 0)
                    {
                        diseaseCount++;
                    }
                    if (prData.ConfirmDate.CompareTo(Convert.ToDateTime("1800-01-01")) > 0)
                    {
                        diseaseCount++;
                    }
                    if (!string.IsNullOrEmpty(prData.ConfirmHospital))
                    {
                        diseaseCount++;
                    }
                }
            }

            int logTopicsCount = 8;
            if (diseaseId > 0)
            {
                HottestLogTopics = LogTopicBLL.Instance.GetLattestLogTopicListByDisease(logTopicsCount, diseaseId);
            }
            else
            {
                HottestLogTopics = LogTopicBLL.Instance.GetHottestLogTopicList(logTopicsCount);
            }

            PatientSearchCondition cond = new PatientSearchCondition();
            cond.DiseaseId=PrData.DiseaseId;
            int Count;
            int symptomIdx = 0;
            foreach (SymptomHistoryInfo item in MyCurrentSymptomList)
            {
                SymptomStatArray[symptomIdx].SymptomId = item.SymptomId;
                SymptomStatArray[symptomIdx].SymptomName = item.SymptomName;
                cond.SymptomId = item.SymptomId;
                PatientBLL.Instance.GetPatientList(1, 1, cond, out Count);
                SymptomStatArray[symptomIdx].PatientCount = Count;
                symptomIdx++;
            }
            cond.SymptomId = -1;
            int treatmentIdx = 0;
            foreach (TreatmentHistoryInfo item in MyCurrentTreatmentList)
            {
                TreatmentStatArray[treatmentIdx].TreatmentId = item.TreatmentId;
                TreatmentStatArray[treatmentIdx].TreatmentName = item.TreatmentName;
                cond.TreatmentId = item.TreatmentId;
                PatientBLL.Instance.GetPatientList(1, 1, cond, out Count);
                TreatmentStatArray[treatmentIdx].PatientCount = Count;
                treatmentIdx++;
            }

            forum = GroupBLL.Instance.GetGroupInfoByName(DiseaseBLL.Instance.GetDiseaseInfo(PrData.DiseaseId).Name);
            HottestTopicsList = TopicBLL.Instance.GetHottestTopicByDiseaseId(PrData.DiseaseId, 5);
            LattestTopicsList = TopicBLL.Instance.GetLastestTopicByDiseaseId(PrData.DiseaseId, 5);
            NewsList = GroupNewsBLL.Instance.GetLattestNews(forum.Id, 5);
            HandlePercentage();

            EmotionLogic emotionLogic = new EmotionLogic();
            allEmotionTypes = emotionLogic.GetAllEmotionTypes();
            emotionData = emotionLogic.GetEmotion(UserData.Id);
            
        }

        private void HandlePercentage()
        {
            if (_User.Birthday.CompareTo(Convert.ToDateTime("1800-01-01 00:00:00.000")) > 0)
            {
                basicCount++;
            }
            if (_User.Gender != "保密")
            {
                basicCount++;
            }
            if (_User.ProvinceId > 0)
            {
                basicCount++;
            }
            symtomCount = MyCurrentSymptomList.Count > 0 ? 1 : 0;
            treatmentCount = MyCurrentAllTreatementList.Count> 0 ? 1:0;
            indicatorCount = MyCurrentIndicatorList.Count > 0 ? 1 : 0;
            overallCount = (basicCount / 3 / 5 + diseaseCount / 3 / 5 + symtomCount / 5 + treatmentCount / 5 + indicatorCount / 5);
            barLength = (overallCount * 300).ToString() + "px";
            overallPercentage = string.Format("{0:0%}", overallCount);
            basicPercentage = string.Format("{0:0%}", basicCount/3);
            diseasePercentage = string.Format("{0:0%}", diseaseCount/3);
            symtomPercentage = string.Format("{0:0%}", symtomCount);
            treatmentPercentage = string.Format("{0:0%}", treatmentCount);
            indicatorPercentage = string.Format("{0:0%}", indicatorCount);
        }
    }
}
